package com.medical_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical_backend.entity.Result;
import com.medical_backend.entity.User;
import com.medical_backend.service.MenuService;
import com.medical_backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    MenuService menuService;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        System.out.println("登陆方法");

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", user.getUserid());
        queryWrapper.eq("password", user.getPassword());

        User serviceOne = userService.getOne(queryWrapper);
        if (serviceOne != null) {
            // 假设你有一个方法可以根据 userid 查权限
            List<String> permissions = menuService.selectMenusByUserid(serviceOne.getUserid());

            // 创建返回的对象，包含用户信息和权限列表
            Map<String, Object> data = new HashMap<>();
            data.put("userid", serviceOne.getUserid());
            data.put("permissions", permissions); // ⬅️ 前端要的权限列表

            return new Result("登录成功", data, 200);
        } else {
            return new Result("登录失败", null, 404);
        }
    }


    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(keyword)) {
            queryWrapper.lambda().like(User::getUserid, keyword).or().like(User::getFullname, keyword);
        }
        Page<User> pageInfo = userService.page(new Page<>(page, size),queryWrapper);
        return new Result("查询成功",pageInfo,200);
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        // 避免前端误传 id，手动置空
        user.setId(null);

        boolean save = userService.save(user);
        if (save) {
            return new Result("新增成功", user, 200);
        } else {
            return new Result("新增失败", null, 500);
        }
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody User user) {
        // 确保传入了 id，用于更新
        if (user.getId() == null) {
            return new Result("用户ID不能为空", null, 400);
        }

        boolean updated = userService.updateById(user);
        if (updated) {
            return new Result("修改成功", user, 200);
        } else {
            return new Result("修改失败", null, 500);
        }
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean isDeleted = userService.removeById(id);
        if (isDeleted) {
            return new Result("删除成功", null, 200);
        } else {
            return new Result("删除失败: 用户不存在", null, 404);
        }
    }
}
